package accs.gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import accs.product.license.License;
import accs.product.update.Updater;
import accs.product.update.Version;

public class SplashScreen extends JDialog {
	public SplashScreen(String imageUrl) {
		super((JFrame)null);
		
		setUndecorated(true);
		//setModal(true);
		setLayout(new BorderLayout());
		setBackground(new Color(0,0,0,0));
		
		
		ImageIcon img = new ImageIcon(getClass().getResource(imageUrl));
		JLabel label=new JLabel(img);
		img.setImageObserver(label);
		setSize(img.getIconWidth(),img.getIconHeight());
		
		add(label, BorderLayout.CENTER);
		setLocationRelativeTo(null);
	}
	
	public static void showSplashScreen(final String imageUrl, final int millis, final SplashListener listener) {
		new Thread() {
			public void run() {
				final SplashScreen scr=new SplashScreen(imageUrl);
				scr.setVisible(true);
				
				try {
					License.loadInstance();
				} catch (Exception e) {
					JOptionPane.showMessageDialog(scr,"Could not load license data! Program will exit.\n"+e.toString(),"Error during startup",JOptionPane.ERROR_MESSAGE);
					System.exit(0);
				}
				
//				try {
//					Version newVersion=new Updater().checkForUpdate();
//					
//					if(newVersion!=null) {
//						scr.setVisible(false);
//						new UpdateDialog(newVersion).setVisible(true);
//						return;
//					}
//				} catch (Exception e) {
//					JOptionPane.showMessageDialog(scr,"Could not load update data! Program will exit.\n"+e.toString(),"Update-Error",JOptionPane.ERROR_MESSAGE);
//					System.exit(0);
//				}
				
				try {
					//Thread.sleep(millis);
				} catch(Exception e) {
					
				}
				scr.setVisible(false);
				listener.finished();
			}
		}.start();
	}
	
	public static interface SplashListener {
		public void finished();
	}
}